package com.cqwu.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadBase;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.cqwu.util.BaseAction;

/**
 * @author LK
 * @time  2015-11-29 下午10:01:01
 */
@SuppressWarnings("serial")
@Namespace("/test")
@ParentPackage("struts-default")
@Controller
@Scope("prototype")
public class TestIntecrptor extends BaseAction{
	
	private String userName;
	private String message = "你已经成功上传了文件";
	
	/**
	 * 使用AjaxFileUpload进行文件上传
	 */
	@Action(value="AjaxUpload",results = {})
	public void ajaxUpload(){
		
		
		String savepath = getServletContext().getRealPath("/WEB-INF/upload");
		try {
		
			DiskFileItemFactory factory = new DiskFileItemFactory();
			//factory.setRepository(new File(this.getServletContext().getr));
			ServletFileUpload upload = new ServletFileUpload(factory);
			upload.setHeaderEncoding("UTF-8");
			if(!upload.isMultipartContent(getRequest())){
				System.out.println("username =  "+ userName);
				return ;
			}
			//upload.setSizeMax(1024*1024);
			List<FileItem> list = upload.parseRequest(getRequest());
			for(FileItem item : list){
				if(item.isFormField()){
					String name = item.getFieldName();
					String value = item.getString("UTF-8");
					System.out.println(name +" = "+ value);
					System.out.println(name + "= " + (new String(value.getBytes("ISO-8859-1"),"UTF-8")));
				}else{
					String filename = item.getName();
					System.out.println(filename);
					if(filename==null || filename.trim().equals("")){
						continue;
					}
					filename = filename.substring(filename.lastIndexOf("\\")+1);
					InputStream fin = item.getInputStream();
					String saveFileName = makeFileName(filename);
					String realSavePath = makePath(saveFileName,savepath);
					FileOutputStream fout = new FileOutputStream(realSavePath+"\\"+saveFileName);
					byte[] buffer = new byte[1024];
					int len = 0;
					while((len = fin.read(buffer))>0){
						fout.write(buffer, 0, len);
					}
					fout.close();
					fin.close();
					item.delete();
				}
			}
			
		}catch (FileUploadBase.FileSizeLimitExceededException e) {
			e.printStackTrace();
			getRequest().setAttribute("message", "文件超出最大值！！！");
			return;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		
	}

	/**
	 * @param saveFileName
	 * @param savepath
	 * @return
	 * TODO
	 */
	private String makePath(String fileName, String savepath) {
		
		int hashcode = fileName.hashCode();
		int dir1 = hashcode&0xf;
		int dir2 = (hashcode&0xf0)>>4;
		String dir = savepath + "\\" +dir1+"\\"+dir2;
		File file = new File(dir);
		if(!file.exists()){
			file.mkdirs();
		}
		return dir;
	}

	/**
	 * @param filename
	 * @return
	 * TODO
	 */
	private String makeFileName(String filename) {
		return UUID.randomUUID().toString()+"_"+filename;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}


	
}
